# figure_5.R

rm(list = ls())
setwd("/Users/John/Dropbox/")

# --- Load Required Packages ---
library(readr)
library(interflex)
library(tidyverse)

# --- Load Data ---
df <- read_csv("JOP_Replication_Materials/data/processed/final_dataset.csv")

# --- Interflex Model ---
intflex <- df %>%
  dplyr::select(isic, isic2, year, strategic, isic_year, median_share, 
         med_hhi_isic2, med_soe_isic2) %>%
  mutate(Strategic = strategic)

intflex <- as.data.frame(intflex)

# --- Estimate with Poisson ---

int.p <- interflex(
  estimator = "linear", 
  Y = "isic_year", 
  D = "Strategic", 
  X = "median_share", 
  Z = c("med_hhi_isic2", "med_soe_isic2"),
  data = intflex, 
  vcov.type = "cluster", 
  cl = c("isic"), 
  na.rm = TRUE, 
  main = "Marginal Effect of Strategic Status on Tech Absorption \nPoisson w/ Cluster Robust SEs", 
  Ylabel = "Tech Absorption Policies", 
  Xlabel = "Median Processing Share", 
  method = "poisson",
  cex.main = 1, 
  ncols = 1, 
  theme.bw = TRUE, 
  nbins = 3, 
  bin.labs = FALSE
)

figure <- predict(int.p,
                  main = "Expected No. of Tech Absorption Policies by Strategic Status\n and Median Processing Share",
                  theme.bw = TRUE,
                  subtitles = c("Not Strategic", "Strategic"),
                  cex.axis = 1,
                  cex.lab = 1.2,
                  cex.sub = 1.2)

plot(figure + theme(plot.margin = margin(10, 10, 10, 10)))

# Manually save as "Dropbox/JOP_Replication_Materials/output/figures/figure_5.pdf" to ensure best figure dimensions





